package com.pine.sunshineelder.eldertreated.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.ElderDao;
import com.pine.sunshineelder.common.dao.ElderTreatedDao;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderEntity;
import com.pine.sunshineelder.common.entity.ElderTreatedEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.eldertreated.ao.ElderTreatedAddAo;
import com.pine.sunshineelder.eldertreated.service.ElderTreatedService;
/**
 * 
 * @author 黄天静
 *
 */
@Service
public class ElderTreatedServiceImpl implements ElderTreatedService, StringHelper {
	@Autowired
	private ElderTreatedDao etdao;

	@Autowired
	private ElderDao edao;

	// @Autowired
	// private DoctorDao ddao;
	@Override
	public ElderTreatedEntity addElderTreated(ElderTreatedAddAo ao) throws Exception {

		// 1.验证和清理参数
		String id = uuid();
		String doctorId = ValidateHelper.$("医生id", ao.getDoctorId());
		String elderId = ValidateHelper.$("老人id", ao.getElderId());
		String content = ValidateHelper.$("检查内容", ao.getContent());
		String frequency = ValidateHelper.$("检查次数", ao.getFrequency());
		LocalDateTime treatmentTime = ao.getTreatmentTime();
		LocalDateTime createTime = LocalDateTime.now();
		// 2.执行业务逻辑
		ElderEntity e = edao.selectById(elderId);
		if (e == null) {
			throw new ThisSystemException("id.not-exisits", "非法老人id");
		}
//		DoctorEntity d = ddao.selectById(elderId);
//		if (d == null) {
//			throw new ThisSystemException("id.not-exisits", "非法医生id");
//		}
		// 3.组装业务结果
		ElderTreatedEntity et = new ElderTreatedEntity();
		et.setId(id);
		et.setElderId(elderId);
		et.setDoctorId(doctorId);
		et.setContent(content);
		et.setFrequency(frequency);
		et.setCreateTime(createTime);
		et.setTreatmentTime(treatmentTime);
		etdao.insert(et);
		return et;
	}

	@Override
	public QueryResult queryBykeyElderTreated(String key, Integer pageNo, Integer pageSize) throws Exception {
		// 1.验证参数
		key = trimAsNull(key);
		// 2.逻辑判断
		if (key != null) {
			// 字符串的拼接，代表前后的数据不限
			key = "%" + key + "%";
		}
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		// 3.组装逻辑结果
		List<ElderTreatedEntity> rows = etdao.selectByKey(key, pageNo, pageSize);
		PageInfo<ElderTreatedEntity> pageInfo = new PageInfo<ElderTreatedEntity>(rows);
		int totalRows = (int) pageInfo.getTotal();
		QueryResult result = new QueryResult();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalRows(totalRows);
		result.setRows(rows);
		return result;
	}

	@Override
	public ElderTreatedEntity queryByIdElderTreated(String id) throws Exception {
		// 1.验证和清理参数
				id = ValidateHelper.$("老人医疗记录id", id);
				// 2执行业务逻辑
				ElderTreatedEntity et = etdao.selectById(id);
				if (et == null) {
					throw new ThisSystemException("id.not-exisits", "非法id");
				}
				return et;
	}

}
